package com.dengcj.epl.service.util;

import cn.hutool.db.Entity;
import com.dengcj.epl.dao.DB;
import com.dengcj.epl.entity.ecology.hrm.User;
import com.dengcj.epl.enums.ecology.workflow.RequestStatus;

/**
 * @author dengchunjie
 * @Description: 工作流工具类
 * @date 2022/1/11 11:55
 */
public class WorkflowUtil {
    /**
     * 根据工作流ID得到对应数据库表名
     * @param workflowid
     * @return
     */
    public static String getTableNameByWorkflowid(int workflowid){
        DB db = new DB();
        Entity entity = db.queryOne("select bll.tablename from workflow_bill bll left join workflow_base bs on bll.id=bs.formid where bs.id=?", workflowid);

        return entity == null ? "" : entity.getStr("tablename");
    }

    /**
     * 根据工作流请求ID获取对应的数据库表名
     * @param requestid
     * @return
     */
    public static String getTableNameByRequestid(int requestid){
        DB db = new DB();
        Entity entity = db.queryOne("select rb.requestid,wf.tablename from WORKFLOW_REQUESTBASE rb left join WORKFLOW_BASE wb on rb.workflowid=wb.id left join WORKFLOW_BILL wf on wf.id=wb.formid where rb.requestid=?", requestid);

        return entity == null ? "" : entity.getStr("tablename");
    }

    /**
     * 获取工作流请求当前节点类型
     * @param requestid
     * @return 0：创建，1：批准，2：提交，3：归档
     */
    public static RequestStatus getRequestCurrentType(int requestid){
        DB db = new DB();
        Entity entity = db.queryOne("select currentnodetype from workflow_requestbase where requestid=?", requestid);

        int index = entity == null ? -1 : entity.getInt("currentnodetype");
        return convert2StatusEnum(index);
    }

    /**
     * 根据requestid获取请求的创建人
     * @param requestid
     * @return
     */
    public static int getRequestCreator(int requestid){
        DB db = new DB();
        Entity entity = db.queryOne("select creater from workflow_requestbase where requestid=?", requestid);

        return entity == null ? -1 : entity.getInt("creater");
    }

    /**
     * 根据workflowid和节点名称获取节点id
     * @param workflowid
     * @param nodename
     * @return
     */
    public static int getNodeIdByWorkflowIdAndNodeName(int workflowid,String nodename){
        DB db = new DB();
        Entity entity = db.queryOne("select a.nodeid from workflow_flownode a inner join workflow_nodebase b on b.id=a.nodeid where a.workflowid=? and b.nodename=?", workflowid, nodename);

        return entity == null ? -1 : entity.getInt("nodeid");
    }

    /**
     * 根据节点ID获取节点名称
     * @param nodeId
     * @return
     */
    public static String getNodeNameById(int nodeId){
        DB db = new DB();
        Entity entity = db.queryOne("select nodename from workflow_nodebase where id=?", nodeId);
        return entity == null ? "" : entity.getStr("nodename");
    }

    /**
     * 根据workflowid和节点名称获取节点id
     * @param requestid
     * @param nodename
     * @return
     */
    public static int getNodeIdByNameAndRequestId(int requestid,String nodename){
        DB db = new DB();
        Entity entity = db.queryOne("select top 1 b.id from workflow_flownode a inner join workflow_nodebase b on a.nodeid=b.id inner join workflow_requestbase c on c.workflowid=a.workflowid where c.requestid=? and nodename=?", requestid, nodename);

        return entity == null ? -1 : entity.getInt("nodeid");
    }

    public static int getStartNodeByRequestId(int requestid){
        DB db = new DB();
        Entity entity = db.queryOne("select b.id as name from workflow_flownode a inner join workflow_nodebase b on a.nodeid=b.id inner join workflow_requestbase c on c.workflowid=a.workflowid where c.requestid=? and b.isstart=1", requestid);

        return entity == null ? -1 : entity.getInt("id");
    }

    /**
     * 获取流程最后操作类型  submit提交  reject退回
     * @param requestid
     * @return
     */
    public static String getRequestLastOperateType(int requestid){
        if("".equals(requestid)){
            return "";
        }

        DB db = new DB();
        Entity entity = db.queryOne("select top 1 operatetype from workflow_requestoperatelog where requestid = ? order by id desc", requestid);

        return entity == null ? "" : entity.getStr("operatetype");
    }

    /**
     * 根据RequestId获取请求编码
     * @param requestId
     * @return
     */
    public static String getRequestNumber(int requestId){
        DB db = new DB();
        Entity entity = db.queryOne("select workflowCode from workflow_codeseqrecord where requestId=?", requestId);

        return entity == null ? "" : entity.getStr("workflowCode");
    }

    public static RequestStatus convert2StatusEnum(int index){
        for (RequestStatus requestStatus : RequestStatus.values()) {
            if(requestStatus.getIndex() == index){
                return requestStatus;
            }
        }

        return null;
    }

    /**
     * 获取流程节点操作者(批准不含退回)
     * @param requestId
     * @param nodeId
     * @return 用户信息, 只包含用户ID和姓名, 其他属性需要单独获取
     */
    public static User getRequestNodeApprovedOperator(int requestId, int nodeId){
        DB db = new DB();
        Entity entity = db.queryOne("select a.userid,b.lastname,b.loginid,b.mobile,b.managerid,b.departmentid,b.subcompanyid1 from workflow_currentoperator a " +
                "inner join hrmresource b on a.userid=b.id " +
                "where requestid=? and nodeid=? and (isreject!=1 or isreject is null)", requestId, nodeId);

        if(entity != null){
            User user = new User();
            user.setId(entity.getInt("userid"));
            user.setLastname(entity.getStr("lastname"));
            user.setLoginId(entity.getStr("loginid"));
            user.setMobile(entity.getStr("mobile"));
            user.setManagerId(entity.getInt("managerid"));
            user.setDepartmentId(entity.getInt("departmentid"));
            user.setSubcompanyId(entity.getInt("subcompanyid1"));

            return user;
        }

        return null;
    }
}
